package com.hexacta.hrs.test.data;

import static com.hexacta.hrs.test.data.DomainTestDataDependencyCreator.HEXACTA_DOMAIN;

import java.util.List;
import java.util.Map;

import org.vectrics.domain.Domain;
import org.vectrics.location.Location;
import org.vectrics.location.LocationType;

/**
 * Test data dependency creator for the Company class.
 * 
 * @author tpollak
 */
public class LocationTestDataDependencyCreator extends AbstractTestDataDependencyCreator {

    public static final String ARGENTINA_LOCATION = "ARGENTINA_LOCATION";

    public static final String BUENOS_AIRES_CITY_LOCATION = "BUENOS_AIRES_CITY_LOCATION";

    public static final String BUENOS_AIRES_PROVINCE_LOCATION = "BUENOS_AIRES_PROVINCE_LOCATION";

    public LocationTestDataDependencyCreator(List<TestDataDependencyCreator> dependencies) {
        super(dependencies);
    }

    public void createDependentTestData(Map<String, Object> context) {
        Domain hexactaDomain = this.loadObjectFromContext(context, HEXACTA_DOMAIN);

        Location argentina = this.createLocation("Argentina", null, LocationType.TYPE_COUNTRY_CODE, hexactaDomain);
        Location buenosAiresProvince = this.createLocation("Buenos Aires", argentina, LocationType.TYPE_STATE_CODE,
                hexactaDomain);
        Location buenosAiresCity = this.createLocation("Capital Federal", buenosAiresProvince,
                LocationType.TYPE_CITY_CODE, hexactaDomain);

        argentina = this.insertDomainObject(argentina);
        buenosAiresProvince = this.insertDomainObject(buenosAiresProvince);
        buenosAiresCity = this.insertDomainObject(buenosAiresCity);

        this.storeObjectReferenceInContext(context, ARGENTINA_LOCATION, argentina);
        this.storeObjectReferenceInContext(context, BUENOS_AIRES_PROVINCE_LOCATION, buenosAiresProvince);
        this.storeObjectReferenceInContext(context, BUENOS_AIRES_CITY_LOCATION, buenosAiresCity);
    }

    private Location createLocation(String name, Location parent, String typeCode, Domain hexactaDomain) {
        Location location = new Location();
        location.setName(name);
        location.setParent(parent);
        location.setTypeCode(typeCode);
        location.setDomain(hexactaDomain);
        location.setApproved(true);

        return location;
    }
}
